package cn.iocoder.yudao.module.design.asyncworker.flowTask;


import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.design.controller.admin.flowtask.vo.FlowTaskCreateReqVO;
import cn.iocoder.yudao.module.design.service.flowtask.FlowTaskService;
import com.tlcsdm.async.callback.ICallback;
import com.tlcsdm.async.callback.IWorker;
import com.tlcsdm.async.executor.timer.SystemClock;
import com.tlcsdm.async.worker.WorkResult;
import com.tlcsdm.async.wrapper.WorkerWrapper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 异步发起任务通知
 * 使用方法见：https://gitee.com/jd-platform-opensource/asyncTool/blob/master/QuickStart.md
 * @author Annie
 * @ClassName FlowTaskNotifyWorker
 * @date 2023年07月29日
 * @version: 1.0
 */
@Slf4j
public class FlowTaskNotifyWorker implements IWorker<String, String>, ICallback<String, String> {

    @Resource
    private FlowTaskService flowTaskService;
    
    @Override
    public void begin() {
        System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
    }

    /**
     * @param success
     * @param param
     * @param workResult
     */
    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        if (success) {
            System.out.println("callback FlowTaskNotifyWorker success--" + SystemClock.now() + "----" + workResult.getResult()
                    + "-threadName:" +Thread.currentThread().getName());
        } else {
            System.err.println("callback FlowTaskNotifyWorker failure--" + SystemClock.now() + "----"  + workResult.getResult()
                    + "-threadName:" +Thread.currentThread().getName());
        }
    }

    @Override
    public String action(String object, Map<String, WorkerWrapper> map) {
        FlowTaskCreateReqVO data = JSONUtil.toBean(object, FlowTaskCreateReqVO.class);
        log.debug("项目： %s  标题：%s ,处理人： %s " ,data.getProjectName(),data.getTaskName(),data.getDealerId());
        Long flowTask = flowTaskService.createFlowTask(data);

        return "FlowTaskNotifyWorker = " + SystemClock.now() + "---result= " + flowTask + " from 1";
    }

    /**
     * @return
     */
    @Override
    public String defaultValue() {
        return "FlowTaskNotifyWorker--default";
    }
}
